package com.yegs.hospital.controller;


import com.yegs.hospital.common.Msg;
import com.yegs.hospital.common.PageVo;
import com.yegs.hospital.service.UserService;
import com.yegs.hospital.util.ConfigConstant;
import com.yegs.hospital.vo.Task;
import com.yegs.hospital.vo.WxUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("/api/user")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 小程序用户登录
     * @return
     */
    @GetMapping("/login")
    public Msg login(@RequestParam("Jscode") String Jscode){
        try {
            WxUser wxUser = userService.login(Jscode);
            return Msg.success(wxUser);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }


    /**
     * 后台用户登录
     * @return
     */
    @PostMapping("/login")
    public Msg login(@RequestBody WxUser wxUser){
        try {
            if("admin".equals(wxUser.getUsername()) && "123456".equals(wxUser.getPassword())){
                return Msg.success("登录成功！");
            }else {
                return Msg.error("账号或密码错误!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }

    /**
     * 保存用户
     * @return
     */
    @PostMapping("/save")
    public Msg save(@RequestBody WxUser wxUser){
        try {
            if(wxUser.getId() == null){
                wxUser.setStatus(ConfigConstant.STA_USER_1);
            }
            userService.save(wxUser);
            return Msg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }

    /**
     * 查询用户
     * @return
     */
    @PostMapping("/list")
    public Msg list(@RequestBody PageVo<WxUser> page){
        try {
            return Msg.success(userService.list(page));
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }



}
